package gewu.suleju.controller;

import gewu.suleju.model.pojo.BrokerInfo;
import gewu.suleju.model.vo.Result;
import gewu.suleju.sevice.imp.BrokerLogAndRegServiceImp;
import gewu.suleju.util.BrokerJWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("manager")
public class ManagerLogController {
    @Autowired
    BrokerLogAndRegServiceImp brokerlogAndReg;
    //  经纪人登陆
    @ResponseBody
    @RequestMapping("managerLogin")
    public Result managerAccount(String managerPhone, String managerPassword) {
        if (managerPhone =="" || managerPassword == "") {
            return new Result("500", "邮箱或密码不能为空");
        }
        //查询登录的人
        BrokerInfo broker = brokerlogAndReg.findBrokerInfoByBrokerPhone(managerPhone);
        if (broker != null) {
            if (managerPassword.equals(broker.getPassword())) {
                String brokerToken = BrokerJWTUtil.geneJsonWebToken(broker);
                return new Result("200", "登陆成功",brokerToken);
            } else {
                return new Result("400", "账户或密码错误");
            }

        }
        return new Result("501", "该用户不存在，请注册");
    }
    //  经纪人注册
    @ResponseBody
    @RequestMapping("managerRegister")
    public Result managerRegister(String managerName, String managerPhone,String managerPassword) {
        if (managerName =="" ||managerPhone == ""||managerPassword=="") {
            return new Result("500", "不能为空");
        }
        //查询注册的人，有则提示登陆
        BrokerInfo broker = brokerlogAndReg.findBrokerInfoByBrokerPhone(managerPhone);
        if (broker != null) {
         return new Result("400", "已注册，请登录");

        }
        BrokerInfo brokerInfo=new BrokerInfo();
        brokerInfo.setBrokerName(managerName);
        brokerInfo.setBrokerPhone(managerPhone);
        brokerInfo.setPassword(managerPassword);
        brokerlogAndReg.SaveBrokerInfo(brokerInfo);
        return new Result("200", "注册成功，请登陆");
    }
    //判断经纪人是否登陆
    @ResponseBody
    @GetMapping("BrokerIsLog")
    public Result BrokerIsLog(HttpServletRequest request, HttpServletResponse response){
        String brokerToken = request.getHeader("brokerToken");
        if(brokerToken.equals(null)||brokerToken.trim().length() == 0) {
            return new Result("500","sorry,请先登录!");
        }
        Claims claims = BrokerJWTUtil.checkJWT(brokerToken);
        BrokerInfo broker= new BrokerInfo();
        broker.setId((Integer) claims.get("brokerId"));
        broker.setBrokerName((String)claims.get("brokerName"));
        return new Result("200","登录成功！",broker);
    }
}
